package com.suyu.controller;

import com.suyu.common.BaseResponse;
import com.suyu.common.ResultUtils;
import com.suyu.model.dto.file.FileDownloadRequest;
import com.suyu.model.dto.file.FileInfoDto;
import com.suyu.service.FileService;
import com.suyu.service.FileStorageService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


/**
 * @author tmh
 * @date 2023/9/24 19:45
 * @description 对文件的controller
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Resource
    private FileService fileService;

    @Resource
    private FileStorageService fileStorageService;

    @ApiOperation("获取所有文件的信息")
    @GetMapping("/getAllFiles")
    public BaseResponse<List<FileInfoDto>> getAllFile() {
        return fileService.getFileInfo();
    }

    @PostMapping("/downloadFile")
    @ApiOperation("下载文件信息")
    public byte[] downLoadFile(@RequestBody FileDownloadRequest fileDownloadRequest) {
        log.info("文件路径：{}",fileDownloadRequest.getFileUrl());
        byte[] bytes = fileStorageService.downLoadFile(fileDownloadRequest.getFileUrl());
        for (byte aByte : bytes) {
            System.out.print(aByte+" ");
        }
        return bytes;
    }

    @PostMapping("/uploadFile")
    @ApiOperation("上传文件信息")
    public BaseResponse<String> upLoadFile(@RequestBody MultipartFile file) throws IOException {
        String url = fileStorageService.uploadImgFile("", file.getOriginalFilename(), file.getInputStream());
        return ResultUtils.success(url);
    }

}
